//index.js
//获取应用实例
var network = require("../../utils/network.js")
const app = getApp()
Page({
    data: {
        sessionId: 0,
        openid: '',
        userName: '123',
        userNumber: '123',
        latitude: 0, //精度
        longitude: 0, //纬度
        activityId: 0,
        res1: {},
        location: [{
                locationContent: '重庆工程学院第6届春季运动会'
            },
            {
                locationContent: '重庆工程学院第二课堂颁奖仪式暨第二课堂启动仪式'
            },
            {
                locationContent: '重庆工程学院第六教学楼201第二届文化艺术节前期工作会议'
            }
        ],
        registerList:[]

    },
    //普通用户向服务登录。获取登录信息sessionId
    login: function(that, f) {
        //   that = this;
        wx.login({
            success: function(res) {
                if (res.code) {
                    //发起网络请求
                    network.request({
                        url: 'https://wxqd.donting.cn/wx/login',
                        data: {
                            code: res.code
                        },
                        showLoadinTitle: '正在登陆....',
                        success: function(res) {
                            var data = res.data;
                            if (data.code == 200) {
                                that.setData({
                                    sessionId: data.data.sessionId,
                                    userName: data.data.userName,
                                    userNumber: data.data.userNumber
                                })
                                f();
                            } else {
                                wx.showModal({
                                    title: '警告',
                                    content: '请先绑定学号',
                                  success(res) {
                                    if (res.confirm) {
                                      wx.navigateTo({
                                        url: '/pages/bindWx/bindWx'
                                      })
                                    } else if (res.cancel) {
                                      console.log('用户点击取消')
                                    }
                                  }
                                });
                           
              
                            }
                        },
                        fail: function(res) {
                            console.log('网络异常---' + res)
                          wx.showModal({
                            title: '警告',
                            content: '网络异常.....',
                          });
                        }
                    })
                } else {
                    console.log('登录失败！' + res.errMsg)
                  wx.showModal({
                    title: '警告',
                    content: '登录失败！',
                  });
                }
            },
            fail: function() {
                wx.showModal({
                    title: '警告',
                    content: '网络异常',
                });
            }
        });
    },
    //后台扫码登录 会更改sessionid 二维码里有sessionId
    backLogin: function(that) {

        console.log('后台扫码登录' + that.data.sessionId)

        that.getLocation(function() {
            wx.login({
                success: function(res) {
                    if (res.code) {
                        //发起网络请求
                        network.request({
                            url: 'https://wxqd.donting.cn/wx/backLogin',
                            data: {
                                code: res.code,
                                latitude: that.data.latitude,
                                longitude: that.data.longitude
                            },
                            showLoadinTitle: "正在登陆....",
                            header: {
                                Cookie: "JSESSIONID=" + that.data.sessionId,
                            },
                            success: function(res) {
                              
                                if(res.data.code==200){
                                    wx.showModal({
                                      title: '警告',
                                      content: '登陆成功',
                                    });
                                }else{
                                  wx.showModal({
                                    title: '警告',
                                    content: res.data.message,
                                  });
                                 
                                }
                            },
                            fail: function(res) {
                                console.log('网络异常---' + res)
                                wx.showModal({
                                    title: '警告',
                                    content: '网络异常',
                                });

                            }
                        })
                    } else {
                        console.log('登录失败！' + res.errMsg)
                    }
                },
                fail: function() {
                    wx.showModal({
                        title: '警告',
                        content: '网络异常',
                    });

                }
            });

        })


    },
    //扫描二维码 
    scanCode: function() {
        var that = this;
        wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                console.log(res.result);
                var res1 = JSON.parse(res.result);
                if (res1.type == 1) {
                    //
                    that.setData({
                        sessionId: res1.sessionId
                    })
                    that.getLocation(function() {
                        network.request({
                            url: 'https://wxqd.donting.cn/wx/registerRQ',
                            data: {
                                latitude: that.data.latitude, //精度
                                longitude: that.data.longitude, //纬度
                            },
                            showLoadinTitle: "正在生成签到信息....",
                            header: {
                                Cookie: "JSESSIONID=" + that.data.sessionId,
                            },
                            success: function(res) {
                                console.log('成功---')
                            },
                            fail: function(res) {
                                console.log('网络异常---' + res)
                            }
                        })
                    });


                }
                if (res1.type == 2) {
                    that.setData({
                        activityId: res1.activityId,
                        res1: res1
                    })
                    //扫码签到
                    that.qiandao(that)
                }
                if (res1.type == 3) {
                    that.setData({

                        sessionId: res1.sessionId
                    })
                    that.backLogin(that);
                }
            },
            fail: (res) => {
                console.log(res);
            }
        })

    },
    //签到
    qiandao: function(that) {
        that.login(that, function() {
            that.getLocation(function() {
                //  latitude:0,//精度
                // longitude: 0,//纬度
                that.data.res1.nowLatitude = that.data.latitude
                that.data.res1.nowLongitude = that.data.longitude
                that.data.res1.nowTime = (new Date()).getTime();
                network.request({
                    url: 'https://wxqd.donting.cn/wx/register',
                    data: {
                        data: that.data.res1
                    },
                    showLoadinTitle: "正在签到....",
                    header: {
                        Cookie: "JSESSIONID=" + that.data.sessionId,
                    },
                    success: function(res) {
                        //签到 接口
                        if (res.data.code == 200) {
                            wx.showToast({
                                title: '成功',
                                icon: 'success',
                                duration: 2000
                            })
                        } else {
                            wx.showModal({
                                title: '警告',
                                content: res.data.message
                            })

                        }
                        console.log('成功---')
                    },
                    fail: function(res) {
                        console.log('网络异常---' + res)
                    }
                })
            });
        })

    },
    //获取位置信息
    getLocation: function(f) {
        var that = this;
      wx.showLoading({
        title: "正在获取位",
      })
        wx.getLocation({
            type: 'wgs84',
            success(res) {
                that.setData({
                    latitude: res.latitude,
                    longitude: res.longitude
                })
              wx.hideLoading();
                console.log("精度：" + that.data.latitude)
                console.log("纬度" + that.data.longitude)
                f();
            },
            fail(res) {
              wx.hideLoading();
              console.log("位置获取失败。。。")
                console.log(res.errMsg)
                wx.showModal({
                    title: '提示',
                    content: '需要获取位置信息',
                    success: function(res) {
                        if (res.confirm) {
                            that.openSetting();
                        }
                    }
                })
            }
        })
    },
    //打开授权界面
    openSetting: function() {
        wx.openSetting({
            success(res) {
                console.log(res.authSetting)

            }
        })
    },
    onLoad:function(){
      var that=this
        this.login(this,function(){
          network.request({
            url: 'https://wxqd.donting.cn/register/findAllRegisterByUserId',
            showLoadinTitle: "正在请求网络......",
            header: {
              Cookie: "JSESSIONID=" + that.data.sessionId,
            },
            success: function (res) {
              //签到 接口
              if (res.data.code == 200) {
                  that.setData({
                    registerList:res.data.data
                  })
              } else {
                wx.showModal({
                  title: '警告',
                  content: res.data.message
                })

              }
              console.log('成功---')
            },
            fail: function (res) {
              console.log('网络异常---' + res)
            }
          })



        })
    },
    // 跳转地图页面
    toMap: function() {
        wx.navigateTo({
            url: '/pages/location/location'
        })
    }

})